package com.languohui.lgh.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.languohui.lgh.domain.*;
import com.languohui.lgh.mapper.*;
import com.languohui.lgh.service.ITenantService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.languohui.lgh.web.controller.dto.TenantDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author author
 * @since 2021-12-29
 */
@Service
public class TenantServiceImpl extends ServiceImpl<TenantMapper, Tenant> implements ITenantService {

    @Autowired
    private MealPermissionMapper mealPermissionMapper;
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private RolePermissionMapper rolePermissionMapper;
    @Autowired
    private TenantMealMapper tenantMealMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private EmployeeRoleMapper employeeRoleMapper;
    @Override
    @Transactional
    public void registerTenant(TenantDto tenantDto) {
        //保存租户表，返回租户ID。mybatisplus默认返回
        Tenant tenant = tenantDto.getTenant();
        tenant.setRegisterTime(System.currentTimeMillis());//设置当前时间
        baseMapper.insert(tenant);
        //添加租户套餐中间表数据
        TenantMeal tenantMeal = new TenantMeal();
        tenantMeal.setTenantId(tenant.getId());
        tenantMeal.setMealId(tenantDto.getMeal());
        tenantMeal.setState(0);
        tenantMeal.setExpireDate(System.currentTimeMillis()+5*24*60*60*1000);//默认5天 单位毫秒
        tenantMealMapper.insert(tenantMeal);
        //创建角色
        //根据套餐ID查询套餐所有权限id
        List<MealPermission> mealPermission = mealPermissionMapper.selectList(
                new QueryWrapper<MealPermission>().eq("meal_id", tenantDto.getMeal())
        );
        Role role = new Role();
        role.setSn("TenantAdmin");
        role.setName("管理员");
        role.setTenant(tenant.getId());
        roleMapper.insert(role);
        List<RolePermission> list = new ArrayList<>();
        for (MealPermission permission : mealPermission) {
            Long permissionId = permission.getPermissionId();
            RolePermission rp = new RolePermission();
            rp.setRoleId(role.getId());
            rp.setPermissionId(permissionId);
            list.add(rp);
        }
        if(list !=null && list.size()>0){
            rolePermissionMapper.insertBatch(list);
        }
        //创建员工 --分配角色

        Employee employee = new Employee();
        employee.setUsername(tenantDto.getUsername());
        employee.setPassword(tenantDto.getPassword());
        employee.setState(0);
        employee.setInputTime(System.currentTimeMillis());
        employee.setTenantId(tenant.getId());
        employee.setType(3);
        employeeMapper.insert(employee);

        //添加员工角色中间表
        EmployeeRole employeeRole = new EmployeeRole();
        employeeRole.setEmployeeId(employee.getId());
        employeeRole.setRoleId(role.getId());
        employeeRoleMapper.insert(employeeRole);

    }
}
